package com.bawei.es.controller;

import com.bawei.common.core.domain.R;
import com.bawei.es.service.EsIndexService;
import com.bawei.system.domain.vo.EsDocDelVo;
import com.bawei.system.domain.vo.EsIndexVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/esIndex")
@Api(value = "ES索引管理",tags = "ES索引管理")
public class EsIndexController {

    @Autowired
    private EsIndexService esIndexService;

    /**
     * 根据索引名称创建索引
     *
     * @return
     */
    @GetMapping("/create")
    @ApiOperation(value = "根据索引名称创建索引")
    public R<Boolean> indexCreate(@RequestParam("indexName") String indexName) {
        return esIndexService.indexCreate(indexName);
    }

    /**
     * 根据索引名称、映射结构创建索引
     *
     * @return
     */
    @PostMapping("/createWithMapping")
    @ApiOperation(value = "根据索引名称、映射结构创建索引")
    public R<Boolean> indexCreateWithMapping(@RequestBody EsIndexVo esIndexVo) {

        return esIndexService.indexCreateWithMapping(esIndexVo);
    }

    /**
     * 根据索引名称查询索引是否存在
     *
     * @return
     */
    @GetMapping("/indexExit")
    @ApiOperation(value = "根据索引名称查询索引是否存在")
    public R<Boolean> indexExit(@RequestParam("indexName") String indexName) {

        return esIndexService.indexExit(indexName);
    }

    /**
     * 根据索引名称删除索引
     *
     * @return
     */
    @GetMapping("/indexDelete")
    @ApiOperation(value = "根据索引名称删除索引")
    public R<Boolean> indexDelete(@RequestParam("indexName") String indexName) {
        return esIndexService.indexDelete(indexName);
    }

    /**
     * 根据索引名称获取mapping
     *
     * @return
     */
    @GetMapping("/indexGetMapping")
    @ApiOperation(value = "根据索引名称删除索引")
    public R<Map> indexGetMapping(@RequestParam("indexName") String indexName) {

        return esIndexService.indexGetMapping(indexName);
    }

    /**
     *根据一个根据索引名称查询详情
     */

    @GetMapping("/getIndexDetails/{indexName}")
    @ApiOperation(value = "根据一个根据索引名称查询详情")
    public R<Map<String, Object>> getIndexDetails(@PathVariable("indexName") String indexName) {
        try {
            Map<String, Object> indexDetails = esIndexService.getIndexDetails(indexName);
            return R.ok(indexDetails);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("Failed to retrieve index details");
        }
    }

    /**
     *添加一个修改索引接口，要可以修改已有的mappings映射
     */
    @PostMapping("/addField")
    @ApiOperation(value = "添加一个修改索引接口，要可以修改已有的mappings映射")
    public R<Map<String, Object>> addFieldToMapping(@RequestBody EsIndexVo esIndexVo) {
        boolean success = esIndexService.addFieldToMapping(esIndexVo);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return R.ok(response);
    }
    /**
     * 删除索引
     */
    @PostMapping("/indexDel")
    @ApiOperation(value = "删除索引")
    public R indexDel(@RequestBody EsDocDelVo esDocDelVo){
        return esIndexService.indexDel(esDocDelVo);
    }

    @GetMapping("/getAllIndex")
    @ApiOperation(value = "查询所有索引")
    public R<List<String>> getAllIndex(){
        List<String> list=esIndexService.getAllIndex();
        return R.ok(list);
    }




}
